package com.qidian.mes.machine.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.qidian.common.pojo.MESResult;
import com.qidian.common.util.JSONTools;
import com.qidian.mes.machine.mapper.MachineLedgerMapper;
import com.qidian.mes.machine.service.MachineLedgerService;
import com.qidian.mes.pojo.Ledger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author 任超
 * Time 2024/10/17 13:39
 */
@RestController
@RequestMapping("/ledger")
@CrossOrigin
public class MachineLedgerController {

    @Autowired
    private MachineLedgerService machineLedgerService;

    @GetMapping("/export")
    public void export(Ledger ledger, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int limit, HttpServletResponse response)throws IOException{
        MESResult result = machineLedgerService.selectList(ledger,page,limit);
        PageInfo<Ledger> pageInfo = (PageInfo<Ledger>) result.getData();
        List<Ledger> ledgers = pageInfo.getList();
        try{
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("台账列表-"+System.currentTimeMillis(),"UTF-8").replaceAll("\\+","%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(),Ledger.class)
                    .sheet("字典数据")
                    .doWrite(ledgers);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(JSONTools.object2json(MESResult.error("下载文件失败")));
        }
    }

    @GetMapping("/list")
    public MESResult list(Ledger ledger, @RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10")int limit){
        return machineLedgerService.selectList(ledger,page,limit);
    }

    @PutMapping("/add")
    public MESResult addLedger(@RequestBody Ledger ledger){
        return machineLedgerService.addLedger(ledger);
    }

    @PutMapping("/updateLedger")
    public MESResult updateLedger(@RequestBody Ledger ledger){
        return machineLedgerService.updateLedger(ledger);
    }

    @GetMapping("/selectById")
    public MESResult selectById(Long machineId){
        return machineLedgerService.selectById(machineId);
    }

    @PutMapping("/updateLedgerStatus")
    public MESResult updateLedgerStatus(Long machineId){
        return machineLedgerService.updateLedgerStatus(machineId);
    }

    @GetMapping("/delete")
    public MESResult delete(int status){
        return machineLedgerService.selectByDelete(status);
    }

    @PutMapping("/updateDeleteStatus")
    public MESResult updateDeleteStatus(Long machineId){
        return machineLedgerService.updateByDelete(machineId);
    }

    @GetMapping("/selectByCode")
    public MESResult selectByCode(String machineCode){
        return machineLedgerService.selectByCode(machineCode);
    }

    @GetMapping("/selectworkShop")
    public MESResult selectWorkshop(Long workShopId){
        return machineLedgerService.selectWorkshop(workShopId);
    }
}
